﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class Account : Telerik.Web.UI.RadAjaxPage
    {
        private void getAllCountries()
        {
            cmbCountry.DataSource = GPSTrackingServerDB.Data.DataRepository.CountriesProvider.GetAll();
            cmbCountry.DataBind();
        }

        private void getAllStates(int CountryId)
        {
            cmbState.DataSource = GPSTrackingServerDB.Data.DataRepository.StatesProvider.GetByCountryId(CountryId);
            cmbState.DataBind();
        }

        private void getAllCities(int StateId)
        {
            cmbCity.DataSource = GPSTrackingServerDB.Data.DataRepository.CitiesProvider.GetByStateId(StateId);
            cmbCity.DataBind();
        }

        private void Invoke_User(string Username)
        {
            GPSTrackingServerDB.Entities.Users userEntity = GPSTrackingServerDB.Data.DataRepository.UsersProvider.GetByUsername(Username);

            txtUsername.Text = userEntity.Username;
            txtPassword.Text = userEntity.Password;

            txtName.Text = userEntity.Name;
            txtFirstName.Text = userEntity.FirstName;
            txtLastName.Text = userEntity.LastName;
            cmbCountry.SelectedValue = userEntity.CountryId.ToString();
            cmbState.SelectedValue = userEntity.StateId.ToString();
            cmbCity.SelectedValue = userEntity.CityId.ToString();
            if (userEntity.Phone != null) txtTelephone.Text = userEntity.Phone.ToString();
            if (userEntity.Mobile != null) txtMobile.Text = userEntity.Mobile.ToString();
            txtEmail.Text = userEntity.Email;

            ViewState["Password"] = userEntity.Password;
            ViewState["CreationDate"] = userEntity.CreationDate;
            ViewState["IsActive"] = userEntity.IsActive;

        
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                getAllCountries();

                getAllStates(Convert.ToInt32(cmbCountry.SelectedValue));

                getAllCities(Convert.ToInt32(cmbState.SelectedValue));

                if (Request.Params["Username"] != null)
                { 
                    Invoke_User(Request.QueryString["Username"]);
                }

            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtPassword.Text != txtRePassword.Text)
                {
                    RadNotification1.Text = "Contraseñas no son iguales";
                    RadNotification1.Show();

                    return;
                }


                GPSTrackingServerDB.Entities.Users userEntity = new GPSTrackingServerDB.Entities.Users();

                userEntity.Username = txtUsername.Text;
                userEntity.OriginalUsername = userEntity.Username;

                if (string.IsNullOrEmpty(txtPassword.Text))
                {
                    userEntity.Password = ViewState["Password"].ToString();
                    txtRePassword.Text = ViewState["Password"].ToString();                    
                }
                else
                    userEntity.Password = txtPassword.Text;

                userEntity.Name = txtName.Text;
                userEntity.FirstName = txtFirstName.Text;
                userEntity.LastName = txtLastName.Text;
                userEntity.CountryId = Convert.ToInt32(cmbCountry.SelectedValue);
                userEntity.StateId = Convert.ToInt32(cmbState.SelectedValue);
                userEntity.CityId = Convert.ToInt32(cmbCity.SelectedValue);                
                if (!string.IsNullOrEmpty(txtTelephone.Text)) userEntity.Phone = Convert.ToDecimal(txtTelephone.Text);
                if (!string.IsNullOrEmpty(txtMobile.Text)) userEntity.Mobile = Convert.ToDecimal(txtMobile.Text);
                userEntity.Email = txtEmail.Text;

                userEntity.CreationDate = Convert.ToDateTime(ViewState["CreationDate"]);
                userEntity.IsActive = Convert.ToBoolean(ViewState["IsActive"]);


                GPSTrackingServerDB.Data.DataRepository.UsersProvider.Update(userEntity);
                RadNotification1.Text = "Guardado";
                RadNotification1.Show();
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Cuenta error ...');", true);
            }
        }

        protected void txtPassword_TextChanged(object sender, EventArgs e)
        {
            txtRePassword.Text = string.Empty;
        }
    }
}